import * as path from 'node:path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

// import legacy from "@vitejs/plugin-legacy";
// gzip
import viteCompression from 'vite-plugin-compression';
import { visualizer } from 'rollup-plugin-visualizer';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import UnoCSS from 'unocss/vite';
import Inspect from 'vite-plugin-inspect';
import { CodeInspectorPlugin } from 'code-inspector-plugin';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  return {
    // define: {
    //   __VUE_OPTIONS_API__: false, // 不使用 Options Api 减小压缩体积
    // },
    plugins: [
      vue(),
      // 兼容IE11
      /* legacy({
          targets: ["ie >= 11"],
          additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
        }), */
      AutoImport({
        /* options */
        include: [
          /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/, // .vue
        ],
        imports: ['vue', 'vue-router', '@vueuse/core'],
        dirs: ['src/hooks', 'src/store', 'src/utils', 'src/api'],
        dts: 'src/auto-import.d.ts',
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        /* options */
        dirs: ['src/components'],
        extensions: ['vue'],
        deep: true,
        dts: 'src/components.d.ts',
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
          }),
        ],
        directoryAsNamespace: true,
      }),
      UnoCSS(),
      ...(command === 'serve'
        ? [
            visualizer(),
            Inspect(),
            CodeInspectorPlugin({
              bundler: 'vite',
              showSwitch: true,
            }),
          ]
        : [
            viteCompression({
              threshold: 102400, // 超过10k进行压缩
            }),
          ]),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/assets/svgs')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',

        /**
         * 自定义插入位置
         * @default: body-last
         */
        // inject?: 'body-last' | 'body-first'

        /**
         * custom dom id
         * @default: __svg__icons__dom__
         */
        // customDomId: '__svg__icons__dom__',
      }),
    ],
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          // 生产环境时移除console
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    test: {
      include: ['tests/unit/*.spec.ts'],
      globals: true,
      environment: 'happy-dom',
      coverage: {
        include: ['src/**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx,vue}'],
        provider: 'istanbul',
        reporter: ['text', 'html'],
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler',
          additionalData: `@use "@/styles/element.theme.scss" as *;`,
        },
        less: {
          modifyVars: {
            'primary-color': '#3476E8',
          },
          javascriptEnabled: true,
        },
      },
    },

    server: {
      hmr: true,
      port: 5000,
      proxy: {
        '/api': {
          target: 'https://site-test-gateway.hrtiger.cn/',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  };
});
